package com.inspur.incubator.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.ComboBoxModel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.inspur.incubator.common.MD5Util;
import com.inspur.incubator.common.Tools;
import com.inspur.incubator.common.interceptor.Page;
import com.inspur.incubator.data.MhUser;
import com.inspur.incubator.data.Role;
import com.inspur.incubator.service.RoleService;
import com.inspur.incubator.service.impl.MhUserImpl;

@Controller
@RequestMapping("/admin")
public class ManageController {

	@Autowired
	private MhUserImpl mhUserImpl;
	@Autowired
	private RoleService roleServiceImpl;
	@RequestMapping("/index")
	public ModelAndView getManage(){
		Map<String, Object> map = new HashMap<>();
		return new ModelAndView("/System/xixian_index", map);
	}


	@RequestMapping("/addAccount")
	public ModelAndView getAccount(){
		return new ModelAndView("/System/addAccount");
		
	}
	@RequestMapping("/accountManage")
	public ModelAndView getManager(MhUser user,Page page) throws UnsupportedEncodingException{
		    List<MhUser> list=mhUserImpl.getMusers(user,page);
	        Map<String,Object> map = new HashMap<String, Object>();
	        map.put("searcher", user);
	        map.put("users", list);
	        map.put("page", page);
	        return new ModelAndView("/System/acmanage", map);
	}
	@RequestMapping("/roleManage")
	public ModelAndView getRoles(){
		 Map<String,Object> parm = new HashMap<String, Object>();
		 List<Role> list=roleServiceImpl.getRoles(parm);
	     Map<String,Object> map = new HashMap<String, Object>();
	        map.put("roles", list);
	    return new ModelAndView("/System/rolemanage", map);
		
	}
	//其他Controller已编写,此处删除
	/*@RequestMapping("/namecheck")
	public ModelAndView getNameCheck(){
		return new ModelAndView("/System/xixian_NameCheck");
	}*/
//	@RequestMapping("/report_presentation")
//	public ModelAndView getPresentation(){
//		return new ModelAndView("/System/xixian_presentation");
//		
//	}
//	@RequestMapping("/report")
//	public ModelAndView getReport(){
//		return new ModelAndView("/System/xixian_report");
//		
//	}
	@RequestMapping("/result")
	public ModelAndView getResult(){
		return new ModelAndView("/System/xixian_ResultsMaintain");
		
	}
	@RequestMapping(value="/admin/registerlinkdo",produces = "text/html;charset=utf-8")
	@ResponseBody
	public String registerlinkdo(MhUser mhUser) {
		String msg = "";
		MhUser mu = new  MhUser();
		mu.setUserName(mhUser.getUserName());
		mu = mhUserImpl.findUser(mu);
		if(mu!=null){
			msg = "该用名已被占用";
		}else{
			mhUser.setUserPassword(MD5Util.ecodeByMD5(mhUser.getUserPassword()));
			int i = mhUserImpl.addUser(mhUser);
			if (i > 0) {
				msg = "注册成功";
			}else{
				msg = "注册失败";
			}
		}
		
		return Tools.toJson(msg);
	}
	@RequestMapping(value="/addUser")
	@ResponseBody
	public String  addMUser(MhUser mu,Page page) {
		String msg = "";
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("page", page);
		MhUser mu1 = mhUserImpl.findMuser(mu);
		if(mu1!=null){
			msg = "该用户名已被占用！";
		}else{
			roleServiceImpl.addUser(mu);
			msg="添加成功！";
		}
		 return msg;
	}
}
